﻿using Dapper;
using Estsh.Core.Base;
using Estsh.Core.Dapper;
using Estsh.Core.Model.Result;
using Estsh.Core.Models;
using Estsh.Core.Repositories;
using Estsh.Core.Wms.IRepositories;
using System.Collections;
using System.Data;
using System.Text;

namespace Estsh.Core.Wms.Repositories
{
    /// <summary>
    /// ERP接口
    /// </summary>
    public class ProductOffLineInterfaceRepository : BaseRepository<BaseEntity>, IProductOffLineInterfaceRepository
    {
        public ProductOffLineInterfaceRepository(DapperDbContext _dapperDbContext) : base(_dapperDbContext)
        {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sapPt"></param>
        /// <returns></returns>
        public WmsInterfaceResult OutPdlineMethod(List<MesWmsHgOutPdlineInterface> sapPts)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                WmsInterfaceResult interfaceResult = new WmsInterfaceResult();
                try
                {
                    StringBuilder SqlStringBuilder = new StringBuilder(1024);
                    List<string> sqlLists = new List<string>();
                    List<DynamicParameters> parameters = new List<DynamicParameters>();
                    for (int i = 0; i < sapPts.Count; i++)
                    {
                        SqlStringBuilder = new StringBuilder(1024);
                        MesWmsHgOutPdlineInterface sapPt = sapPts[i];
                        SqlStringBuilder.Append("INSERT INTO dbo.MES_WMS_HG_OUT_PDLINE ");
                        SqlStringBuilder.Append("           (work_order_no,pdline_code,part_no,serial_number,qty,lot_no ,product_type ");
                        SqlStringBuilder.Append("           ,product_version,factory_code,syn_flg");
                        SqlStringBuilder.Append("           ,create_userid,create_time,guid,IsRejects) ");
                        SqlStringBuilder.Append("     VALUES(@work_order_no,@pdline_code,@part_no,@serial_number,@qty,@lot_no ");
                        SqlStringBuilder.Append("           ,@product_type,@product_version,@factory_code,@syn_flg ");
                        SqlStringBuilder.Append("           ,@create_userid,CONVERT(varchar(50), GETDATE(), 21) ");
                        SqlStringBuilder.Append("           ,@guid,@IsRejects) ");
                        sqlLists.Add(SqlStringBuilder.ToString());

                        DynamicParameters dynamic = new DynamicParameters();
                        dynamic.Add("@work_order_no", sapPt.work_order_no);
                        dynamic.Add("@pdline_code", sapPt.pdline_code);
                        dynamic.Add("@part_no", sapPt.part_no);
                        dynamic.Add("@serial_number", sapPt.serial_number);
                        dynamic.Add("@qty", sapPt.Qty);
                        dynamic.Add("@lot_no", sapPt.lot_no);
                        dynamic.Add("@product_type", sapPt.product_type);
                        dynamic.Add("@product_version", sapPt.product_version);
                        dynamic.Add("@factory_code", sapPt.factory_code);
                        dynamic.Add("@syn_flg", "10");
                        dynamic.Add("@create_userid", "0");
                        dynamic.Add("@guid", Guid.NewGuid().ToString());
                        dynamic.Add("@IsRejects", sapPt.IsRejects);
                        parameters.Add(dynamic);
                    }
                    bool result = this.ExecuteTransaction(sqlLists, parameters);
                    if (result == true)
                    {
                        interfaceResult.success = true;
                        interfaceResult.message = "成功";
                    }
                    else
                    {
                        interfaceResult.success = false;
                        interfaceResult.message = "数据添加失败，请检查";
                    }
                }
                catch (Exception ex)
                {
                    interfaceResult.success = false;
                    interfaceResult.message = ex.Message.ToString();
                }
                return interfaceResult;
            }
        }

    }
}
